<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxGameOpen;
use think\Db;

class BoxGameOpenService extends BaseService
{
    // 设备类型,平台内容根据当前值做判定
    protected $device = [
        'ios'       => 1,
        'android'   => 2,
        'pc'        => 3,
    ];

    protected $adDevice = [
        'android'   => 2,
        'ios'       => 3,
        'pad'       => 4,
        'ipad'      => 5,
        'pc'        => 6
    ];
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new BoxGameOpen();
    }

    public function getList(): array
    {
        $param = request()->param();
        $map   = [];

        //区服ID搜索
        if (isset($param['status']) && $param['status']) {
            $map[] = ['o.status', '=', $param['status']];
        }
        //区服名称搜索
        if (isset($param['game_name']) && $param['game_name']) {
            $map[] = ['g.game_name', 'like', '%' . $param['game_name'] . '%'];
        }
        // $data = parent::getList($map);
        $map[] = ['o.mark', '=', 1];
        $result = $this->model->alias('o')
            ->leftJoin('box_game g', 'g.id=o.game_id')
            ->field(['o.*', 'g.game_name', 'g.id game_id','g.icon']);

        if (isset($param['device']) && $param['device']) {
            $device = $param['device'];
            $result = $result->where(function ($query) use ($device) {
                $query->where(Db::raw("FIND_IN_SET('0',g.on_platform)"));
                $query->whereOr(Db::raw("FIND_IN_SET('{$device}',g.on_platform)"));
            });
        }

        $result = $result->where($map)
            ->where($map)->order("o.id DESC")
            ->paginate($param['limit'])->toArray();
        array_walk($result['data'], function (&$v) {
            if(!empty($v['media'])){
                $v['media'] = [json_decode($v['media'], true)];
            }else{
                $v['media'] = [];
            }
        });
        return [
            "msg" => '操作成功',
            "code" => 0,
            "data" => $result['data'] ?? [],
            "count" => $result['total'] ?? 0,
        ];

        // if ($data['count'] <= 0) return $data;
        // $game_ids = array_column($data['data'], 'game_id');
        // $games    = BoxGame::where('id', 'in', $game_ids)->select();
        // $games    = array_column($games ? $games->toArray() : [], null, 'id');
        //
        // foreach ($data['data'] as $k => $i) {
        //     $data['data'][$k]['game_info'] = $games[$i['game_id']] ?? [];
        // }
        // return $data;
    }

    public function edit()
    {
        $data = request()->param();
        if (!isset($data['game_id']) || empty($data['game_id'])) {
            return message('请选择游戏', false);
        }
        if (!isset($data['name']) || empty($data['name'])) {
            return message('请输入区服名称', false);
        }
        if (!isset($data['status']) || empty($data['status'])) {
            return message('请选择状态', false);
        }
        if (!isset($data['open_time']) || empty($data['open_time'])) {
            return message('请选择开服时间', false);
        }
        $data['open_time'] = strtotime($data['open_time']);
        $res               = $this->model->where(['game_id' => $data['game_id'], 'server_id' => $data['server_id'], 'mark' => 1])->find();
        $server_id         = $res['server_id'] ?? '';
        if (empty($data['id']) && !empty($res['id'])) {
            return message('当前游戏的区服已经存在,请输入不同的区服ID', false);
        }
        $name = $this->model->field('id')->where(['name' => $data['name'], 'mark' => 1])->find();
        //编辑
        if (!empty($data['id'])) {
            if (!empty($server_id) && $res['id'] != $data['id']) return message('当前游戏的区服已经存在,你输入不同的区服ID', false);
            if (!empty($name) && $name['id'] != $data['id']) return message('当前游戏的区服名称已经存在,你输入不同的区服名称', false);
        }
        if (empty($data['id']) && !empty($name['id'])) {
            return message('当前游戏的区服名称已经存在,你输入不同的区服名称', false);
        }
        $data['media'] = json_encode($data['media'] ?? []);
        return parent::edit($data); // TODO: Change the autogenerated stub
    }

    /**
     * 获取对应游戏开服列表
     * @return array
     */
    public function gameOpenCommonMenuList()
    {
        $gameList = $this->model->field(['server_id', 'name'])->where([['game_id', '=', request()->post('game_id')]])->order("id DESC")->select();

        return message("操作成功", true, $gameList);
    }
}